﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
using ComponentArt.Silverlight.UI.Navigation;

namespace MapEditor
{
    public class ObjectTemplateMgr
    {
        public List<Object> Objects = new List<Object>();

        private void SetDefaults(Object obj, XElement element)
        {
            if (null != element.Element(Consts.DescAttribute))
                obj.Description = element.Attribute(Consts.DescAttribute).Value;
            if (null != element.Element(Consts.NameAttribute))
                obj.ObjectName = element.Element(Consts.NameAttribute).Value;
            if (null != element.Element(Consts.WalkableAttribute))
            {
                if (element.Element(Consts.WalkableAttribute).Value.ToLower() == Consts.False)
                    obj.IsWalkable = false;
                else
                    obj.IsWalkable = true;
            }
        }

        private TerrainObject CreateTerrainObject(XElement element)
        {
            TerrainObject terrainObject = new TerrainObject(0, 0, Consts.DefaultTileRadius, Consts.TerrainLayerCount);
            SetDefaults(terrainObject, element);

            if (null != element.Element(Consts.ImageAttribute))
                terrainObject.SetImage(0, element.Element(Consts.ImageAttribute).Value);

            return terrainObject;
        }

        private GameObject CreateGameObject(XElement element)
        {
            GameObject gameObject = new GameObject(0,0,1);
            SetDefaults(gameObject, element);
            return gameObject;
        }

        private MapObject CreateMapObject(XElement element)
        {
            int frame = 0;
            List<string> fileNames = new List<string>();
            foreach(XElement imageElement in element.Descendants(Consts.ImageAttribute))
                fileNames.Add(imageElement.Value);

            MapObject mapObject = new MapObject(0,0,fileNames.Count);
            SetDefaults(mapObject, element);

            for(int i=0; i < fileNames.Count; i++)
                mapObject.SetImage(frame++, fileNames[i]);
            
            return mapObject;
        }

        private CreatureObject CreateCreature(XElement element)
        {
            CreatureObject creature = new CreatureObject(0,0);
            SetDefaults(creature, element);

            creature.SetWidth(Convert.ToInt32(element.Attribute(Consts.WidthAttribute).Value));
            creature.SetHeight(Convert.ToInt32(element.Attribute(Consts.HeightAttribute).Value));

            foreach (XElement imageElement in element.Descendants(Consts.ImageAttribute))
            {
                int frameCount = Convert.ToInt32(imageElement.Attribute(Consts.FrameCountAttribute).Value);
                int framesPerRow = Convert.ToInt32(imageElement.Attribute(Consts.FramesPerRowAttribute).Value);
                string direction = imageElement.Attribute(Consts.DirectionAttribute).Value;
                string animationType = imageElement.Attribute(Consts.TypeAttribute).Value;
                Enums.AnimationType type = Utils.GetAnimationType(animationType);
                Enums.Direction dir = Utils.GetDirection(direction);
               
                creature.AddImageFrame(dir, type, frameCount, framesPerRow, imageElement.Value);
            }
            return creature;

        }

        public void LoadTemplateObjects(string fileName)
        {
            XDocument document = XDocument.Load(fileName);

            foreach (XElement element in document.Descendants(Consts.ObjectAtrribute))
            {
                try
                {
                    string type = element.Attribute(Consts.TypeAttribute).Value;
                    if (type == Consts.TerrainAttribute)
                    {
                        TerrainObject terrainObj = CreateTerrainObject(element);
                        if (null != terrainObj)
                            Objects.Add(terrainObj);
                    }
                    else if (type == Consts.GameAttribute)
                    {
                        GameObject gameObj = CreateGameObject(element);
                        if (null != gameObj)
                            Objects.Add(gameObj);
                    }
                    else if (type == Consts.MapAttribute)
                    {
                        MapObject mapObj = CreateMapObject(element);
                        if (null != mapObj)
                            Objects.Add(mapObj);
                    }
                    else if (type == Consts.CreatureAttribute)
                    {
                        CreatureObject creature = CreateCreature(element);
                        if (null != creature)
                            Objects.Add(creature);
                    }
                }
                catch { }
            }
        }
    }
}
